package com.jy.adscm.serviceProvider.service.impl;

import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.dealer.bean.Dealer;
import com.jy.adscm.dealer.bean.DealerServiceRelation;
import com.jy.adscm.dealer.mapper.DealerMapper;
import com.jy.adscm.dealer.mapper.DealerServiceRelationMapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.hospital.bean.JyHospitalBase;
import com.jy.adscm.hospital.mapper.JyHospitalBaseMapper;
import com.jy.adscm.order.bean.JyOrder;
import com.jy.adscm.order.bean.JyProduct;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.serviceProvider.bean.JyService;
import com.jy.adscm.serviceProvider.bean.JyServiceType;
import com.jy.adscm.serviceProvider.mapper.JyServiceMapper;
import com.jy.adscm.serviceProvider.mapper.JyServiceTypeMapper;
import com.jy.adscm.serviceProvider.service.JyServiceService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-01-24
 */
@Service
public class JyServiceServiceImpl extends ServiceImpl<JyServiceMapper, JyService> implements JyServiceService {
    @Autowired
    DealerMapper dealerMapper;
    @Autowired
    JyHospitalBaseMapper jyHospitalBaseMapper;
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    JyCodeMapper jyCodeMapper;
    @Autowired
    JyServiceTypeMapper jyServiceTypeMapper;
    @Autowired
    DealerServiceRelationMapper dealerServiceRelationMapper;
    @Transactional
    @Override
    public boolean insertJyService(JyService jyService, Token token) throws AdscmSystemException {
        jyService.setApplicant(token.getUser().getId().toString());
        jyService.setApplicantTime(new Date());
        jyService.setDealer(token.getUser().getUnitId().toString());
        jyService.setServiceState(0);
        boolean insert = super.insert(jyService);
        return insert;
    }
    @Transactional
    @Override
    public boolean deleteJyService(Integer id, Token token) throws AdscmSystemException {
        JyService jyService = super.selectById(id);
        if(jyService.getServiceState()!=0){
            throw new AdscmSystemException("不能删除这条信息");
        }
        boolean b = super.deleteById(id);
        return b;
    }
    @Transactional
    @Override
    public Boolean updateJyService(JyService jyService, Token token) throws AdscmSystemException {
        //待审核
        if (jyService.getServiceState()==0 && AdscmBaseUtils.getInstance().isNotEmpty(jyService.getApproveResult(),null)&&
             AdscmBaseUtils.getInstance().isNotEmpty(jyService.getServicer(),null)){
            jyService.setApprover(token.getUser().getId().toString());
            jyService.setApproveTime(new Date());
            if ("1".equals(jyService.getApproveResult())){
                jyService.setServiceState(1);
            }
        }
        //待评价
        if (jyService.getServiceState()==1 && AdscmBaseUtils.getInstance().isNotEmpty(jyService.getServiceEvaluation(),null)){
            jyService.setServiceState(2);
        }
        boolean b = super.updateById(jyService);
        return b;
    }
    @Override
    public List<JyService> selectAll(Token token) throws AdscmSystemException {
        List<JyService> jyServices = new ArrayList<>();

        if (token.getUser().getUnitId()==0){
            //厂家的用户
                Wrapper<JyService> wrapper =new EntityWrapper<>();
                jyServices = super.selectList(wrapper);
            }
        else {
            //经销商
                Wrapper<JyService> wrapper =new EntityWrapper<>();
                wrapper.eq("dealer",token.getUser().getUnitId().toString());
                jyServices = super.selectList(wrapper);
            }
        for (JyService jyService : jyServices) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(jyService.getApplicant(),null)){
                JyUser jyUser = jyUserMapper.selectById(jyService.getApplicant());
                jyService.setApplicantName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jyService.getApprover(),null)){
                JyUser jyUser = jyUserMapper.selectById(jyService.getApplicant());
                jyService.setApproverName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jyService.getDealer(),null)){
                Dealer dealer = dealerMapper.selectById(jyService.getDealer());
                jyService.setDealerName(dealer.getName());

            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jyService.getHospital(),null)){
                JyHospitalBase jyHospitalBase = jyHospitalBaseMapper.selectById(jyService.getHospital());
                jyService.setHospitalName(jyHospitalBase.getHospitalName());
            }
             if (AdscmBaseUtils.getInstance().isNotEmpty(jyService.getDepartment(),null)){
                 JyCode jyCode =new JyCode();
                 jyCode.setCode(jyService.getDepartment());
                 jyCode.setType(4);
                 jyCode=jyCodeMapper.selectOne(jyCode);
                 jyService.setDepartmentName(jyCode.getName());
             }
             if (AdscmBaseUtils.getInstance().isNotEmpty(jyService.getServiceState(),null)){
                 JyCode jyCode =new JyCode();
                 jyCode.setCode( String.valueOf(jyService.getServiceState()) );
                 jyCode.setType(8);
                 jyCode = jyCodeMapper.selectOne(jyCode);
                 jyService.setServiceStateName(jyCode.getName());
             }
             if (AdscmBaseUtils.getInstance().isNotEmpty(jyService.getServiceType(),null)){
                 JyServiceType jyServiceType = jyServiceTypeMapper.selectById(jyService.getServiceType());
                 jyService.setServiceTypeName(jyServiceType.getServiceName());
             }
             if (AdscmBaseUtils.getInstance().isNotEmpty(jyService.getLastServiceDept(),null)){
                Dealer dealer = dealerMapper.selectById(jyService.getLastServiceDept());
                jyService.setLastServiceDeptName(dealer.getName());
            }


        }
        return jyServices;
    }

    @Override
    public List<Dealer> queryLastServiceDept(String serviceType, Token token) {
        //查询这个这个服务有有哪些供应商
        List<Dealer> dealers=new ArrayList<>();
        Wrapper<DealerServiceRelation> dealerServiceRelationWrapper=new EntityWrapper<>();
        dealerServiceRelationWrapper.eq("service_id",serviceType);
        List<DealerServiceRelation> dealerServiceRelations = dealerServiceRelationMapper.selectList(dealerServiceRelationWrapper);
        for (DealerServiceRelation dealerServiceRelation : dealerServiceRelations) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(dealerServiceRelation.getDid(),null)){
                Dealer dealer = dealerMapper.selectById(dealerServiceRelation.getDid());
                if (AdscmBaseUtils.getInstance().isNotEmpty(dealer,null)){
                    dealers.add(dealer);
                }
            }
        }
//        Wrapper<Dealer> dealerWrapper=new EntityWrapper<>();
//        dealerWrapper.eq("is_manufacturer",1);
//        List<Dealer> dealers1 = dealerMapper.selectList(dealerWrapper);
//        for (Dealer dealer : dealers1) {
//            if (AdscmBaseUtils.getInstance().isNotEmpty(dealer,null)){
//                dealers.add(dealer);
//            }
//        }
        return dealers;
    }
}
